home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / defroi.pro < prev    next >
Text File  |  1997-07-08  |  5KB  |  150 lines

  1. ; $Id: defroi.pro,v 1.4 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1987-1997, Research Systems, Inc.  All rights reserved.
  4. ;    Unauthorized reproduction prohibited.
  5. ;
  6.  
  7. Function Defroi, Sx, Sy, Xverts, Yverts, X0=x0, Y0=y0, ZOOM = ZOOM, $
  8.     NOREGION = Noregion, NOFILL = Nofill, RESTORE = restore
  9.  ;Define an irregular Region of Interest.
  10. ;+
  11. ; NAME:            DEFROI
  12. ; PURPOSE:    Define an irregular region of interest of an image
  13. ;        using the image display system and the cursor/mouse.
  14. ; CATEGORY:    Image processing.
  15. ; CALLING SEQUENCE:
  16. ;    R = Defroi(Sx, Sy, X0, Y0)
  17. ; INPUTS:
  18. ;    Sx, Sy = Size of image, in pixels.
  19. ; Optional Inputs:
  20. ;    X0, Y0 = Coordinate of Lower left corner of image on display.
  21. ;    If omitted, (0,0) is assumed.  Screen device coordinates.
  22. ;    ZOOM = zoom factor, if omitted, 1 is assumed.
  23. ; OUTPUTS:
  24. ;    Function result = vector of subscripts of pixels inside the region.
  25. ;    Side effect: The lowest bit in which the write mask is enabled
  26. ;    is changed.
  27. ; OPTIONAL OUTPUTS:
  28. ;    Xverts, Yverts = Optional output parameters which will contain
  29. ;        the vertices enclosing the region.
  30. ; KEYWORD Parameters:
  31. ;    NOREGION = Setting NOREGION inhibits the return of the
  32. ;        pixel subscripts.
  33. ;    NOFILL = if set, inhibits filling of irregular region on completion.
  34. ;    RESTORE = if set, original image on display is restored to its
  35. ;        original state on completion. 
  36. ; COMMON BLOCKS:
  37. ;    None.
  38. ; SIDE EFFECTS:
  39. ;    Display is changed if RESTORE is not set.
  40. ; RESTRICTIONS:
  41. ;    Only works for interactive, pixel oriented devices with a
  42. ;        cursor and an exclusive or writing mode.
  43. ;    A region may have at most 1000 vertices.  If this is not enough
  44. ;        edit the line setting MAXPNTS.
  45. ; PROCEDURE:
  46. ;    The exclusive or drawing mode is used to allow drawing and
  47. ;    erasing objects over the original object.
  48. ;
  49. ;    The operator marks the vertices of the region, either by
  50. ;        dragging the mouse with the left button depressed or by
  51. ;        marking vertices of an irregular polygon by clicking the
  52. ;        left mouse button, or with a combination of both.
  53. ;    The center button removes the most recently drawn points.
  54. ;    Press the right mouse button when finished.
  55. ;    When the operator is finished, the region is filled using 
  56. ;        the polyfill function, and the polyfillv function is used
  57. ;        to compute the subscripts within the region.
  58. ;
  59. ; MODIFICATION HISTORY:  DMS, March, 1987.
  60. ;     Revised for SunView, DMS, Nov, 1987.
  61. ;       Added additional argument checking, SNG April, 1991
  62. ;    Modified for devices without write masks: DMS, March, 1992.
  63. ;        Uses exclusive or mode rather than write masks.
  64. ;    
  65. ;-
  66. ;
  67. on_error,2        ;Return to caller if error
  68. nc1 = !d.table_size-1    ;# of colors available
  69.  
  70. if sx lt 1 or sy lt 1 then $        ;Check some obvious things
  71.     message, 'Dimensions of the region must be greater than zero.'
  72.  
  73. if sx gt !d.x_size then $
  74.     message, 'The width of the region must be less than ' + $
  75.     strtrim(string(!d.x_size),2)
  76.  
  77. sy = sy < !d.y_size
  78.  
  79. device, set_graphics=6             ;Set XOR mode
  80. again:
  81. n = 0
  82. print,'Left button to mark point'
  83. print,'Middle button to erase previous point'
  84. print,'Right button to close region'
  85. maxpnts = 1000            ;max # of points.
  86. xverts = intarr(maxpnts)        ;arrays
  87. yverts = intarr(maxpnts)
  88. xprev = -1
  89. yprev = -1
  90. if n_elements(x0) le 0 then x0 = 0
  91. if n_elements(y0) le 0 then y0 = 0
  92. if n_elements(zoom) le 0 then zoom = 1
  93. ;
  94. Cursor, xx, yy, /WAIT, /DEV        ;Get 1st point with wait
  95. repeat begin
  96.     xx = (xx - x0) / zoom    ;To image coords
  97.     yy = (yy - y0) / zoom
  98.     if (xx lt sx) and (yy lt sy) and (!err eq 1) and $
  99.         ((xx ne xprev) or (yy ne yprev)) then begin        ;New point?
  100.         xprev = xx
  101.         yprev = yy
  102.         if n ge (maxpnts-1) then begin
  103.             print,'Too many points'
  104.             n = n-1
  105.             endif
  106.         xverts[n] = xx
  107.         yverts[n] = yy
  108.         if n ne 0 then $
  109.           plots,xverts[n-1:n]*zoom+x0,yverts[n-1:n]*zoom + y0, $
  110.             /dev,color=nc1,/noclip
  111.         n = n + 1
  112.         endif
  113. ;        We use 2 or 5 for the middle button because some Microsoft
  114. ;        compatible mice use 5.
  115.     if ((!err eq 2) or (!err eq 5)) and (n gt 0) then begin
  116.         n = n-1
  117.         if n gt 0 then begin  ;Remove a vertex
  118.           plots,xverts[n-1:n]*zoom+x0,yverts[n-1:n]*zoom+y0,color=nc1,$
  119.             /dev,/noclip
  120.                   wait, .1           ;Dont erase too fast
  121.           endif
  122.     endif
  123.     Cursor, xx, yy, /WAIT, /DEV    ;get x,y, no wait, device coords.
  124.     endrep until !err eq 4
  125.  
  126. if n lt 3 then begin
  127.     print,'ROI - Must have 3 points for region.  Try again.'
  128.     goto,again
  129.     endif
  130. xverts = xverts[0:n-1]        ;truncate
  131. yverts = yverts[0:n-1]
  132.  
  133. if keyword_set(restore) then $
  134.     plots, xverts*zoom+x0, yverts *zoom + y0, /dev, color=nc1, /noclip $
  135. else if keyword_set(nofill) then $
  136.      plots, [xverts[0],xverts[n-1]]*zoom+x0, $
  137.            [yverts[0],yverts[n-1]]*zoom+y0,$
  138.         /dev,color = nc1,/noclip   $     ;Complete polygon
  139. else polyfill, xverts*zoom+x0, yverts*zoom+y0,$
  140.         /dev,color = nc1,/noclip ;Complete polygon
  141.  
  142. if !order ne 0 then yverts = sy/zoom - 1 - yverts    ;Invert Y?
  143. device,set_graphics=3   ;Re-enable normal copy write
  144.  
  145. if keyword_set(noregion) then a = 0 $
  146.   else a = polyfillv(xverts,yverts,sx,sy)    ; get subscripts inside area.
  147.  
  148. return,a
  149. end
  150.